import 'package:acgn_pioneer/components/book-item/book1.dart';
import 'package:acgn_pioneer/components/circle_tag.dart';
import 'package:acgn_pioneer/models/author_model.dart';
import 'package:acgn_pioneer/pages/reader/reader_page_main.dart';
import 'package:acgn_pioneer/utils/pop_util.dart';
import 'package:acgn_pioneer/widgets/reader/services/sr_comic.dart';
import 'package:expandable_text/expandable_text.dart';
import 'package:flutter/widgets.dart';

import '../../public.dart';

/// 作者介绍
class ReaderAuthorDetail extends StatefulWidget {
  final ComicModel comicInfo;

  ReaderAuthorDetail({Key key, this.comicInfo}) : super(key: key);

  @override
  ReaderAuthorDetailState createState() => new ReaderAuthorDetailState();
}

class ReaderAuthorDetailState extends State<ReaderAuthorDetail> {
  AuthorModel authorModel;

  @override
  void initState() {
    super.initState();

    getRelaComic();
  }

  @override
  void dispose() {
    super.dispose();
  }

  getRelaComic() async {
    var re =
        await ReaderComicService().getComicinfoRoleDetail(widget.comicInfo.id);
    authorModel =
        re.firstWhere((item) => item.name == widget.comicInfo.authorName);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    if (authorModel == null) {
      return Container();
    }
    return buildGridView();
  }

  /// 构建目录区域
  buildGridView() {
    return Container(
        padding: EdgeInsets.all(10),
        child: Column(children: [
          Container(
            padding: EdgeInsets.only(top: 10),
            child: Row(
              children: [
                ClipRRect(
                    borderRadius: BorderRadius.circular(20),
                    child: Image.network(
                      authorModel.sculpture,
                      fit: BoxFit.cover,
                      width: Rem(40),
                      height: Rem(40),
                    )),
                SizedBox(width: 8),
                Text(
                  authorModel.name,
                  textAlign: TextAlign.start,
                  style: TextStyle(
                      color: ColorUtil.rgba(255, 255, 255, 0.6), fontSize: 14),
                ),
                SizedBox(width: 8),
                CircleTag(
                    text: '作者',
                    textColor: Colors.white,
                    bgColor: ColorUtil.rgba(254, 45, 102, 1))
              ],
            ),
          ),
          authorModel.summary != ''
              ? Container(
                  margin: EdgeInsets.only(top: Rem(16), bottom: Rem(10)),
                  constraints: BoxConstraints(
                    maxHeight: Rem(80),
                  ),
                  child: ExpandableText(authorModel.summary,
                      expandText: '展开',
                      collapseText: '',
                      maxLines: 2,
                      style: TextStyle(
                          color: ColorUtil.rgba(255, 255, 255, 0.6),
                          fontSize: 14),
                      linkColor: ColorUtil.rgba(100, 145, 217, 1)),
                )
              : Container(),
          Container(
              height: Rem(320),
              child: GridView.builder(
                  shrinkWrap: true,
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 3,
                      childAspectRatio: 105 / 170,
                      mainAxisSpacing: 6),
                  itemCount: authorModel.relatedComics.length,
                  itemBuilder: (context, index) {
                    var comic = authorModel.relatedComics[index];
                    return GestureDetector(
                      child: Book1Component(
                          cover: comic.comicCover34,
                          title: comic.cartoonName,
                          desc: comic.lastChapterName),
                      onTap: () {
                        PopUtil.showSlidePop(
                            context: context,
                            child: ReaderPageMain(
                                comicIds: authorModel.relatedComics
                                    .map((e) => e.cartoonId)
                                    .toList(),
                                comicId: comic.cartoonId,
                                isPopNested: true));
                      },
                    );
                  }))
        ]));
  }
}
